package org.mr.disit.lessonrecorder
{
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.NativeProcessExitEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.Socket;
	import flash.net.sendToURL;
	import flash.utils.setTimeout;
	
	import mx.controls.Alert;
	
	public class VLCRemoteControl extends EventDispatcher{
		
		private var s:Socket;
		public var targetFile:File;
		private var port:int;
		private var name:String;
		
		public function VLCRemoteControl(instName:String){
			this.name=instName;
			s= new Socket();
		}
		
		
		
		public function init(portNum:int,confFile:File,videoFile:File):void{
			//disconnect();
			this.port=portNum;
			this.targetFile=videoFile;
			if(targetFile.exists){
				targetFile.deleteFile();
			}
			if (confFile.exists)
				prepareFromFile(confFile);
			else Alert.show("Cannot find config file for stream " + name);
			try{
				s  = new Socket() ;
			}
			catch (e:Error) {
				trace("Error: " + e.toString());
				Alert.show(s.remoteAddress + ":" + s.remotePort);
				Alert.show(s.readUTFBytes(s.bytesAvailable));
				
			}
			
		}
		
		private function recv(event:Event):void{
			if (event.target.bytesAvailable)
				Alert.show(event.target.readUTFBytes(event.target.bytesAvailable),name);
			 s.removeEventListener(ProgressEvent.SOCKET_DATA,recv);
		
		}
		
		public function connect():void{
			if( !s.connected) {
				try {
					s.connect("localhost",this.port);
					s.writeUTFBytes("air\n");
					// s.addEventListener(ProgressEvent.SOCKET_DATA,recv);
					s.flush();
				}
				catch (e:Error) {
					trace("Error: " + e.toString());
					Alert.show(s.remoteAddress + ":" +s.remotePort);
					Alert.show(s.readUTFBytes(s.bytesAvailable));
					
				}
			}
		}
		
		private function prepareFromFile(confFile:File):void{
			var stream:FileStream = new FileStream();
			stream.open(confFile, FileMode.READ);
			var line:String  = stream.readUTFBytes(stream.bytesAvailable) ;
			this.send(line.replace('$$',targetFile.nativePath));
		}
		
		public function addMedia():void{
			this.send("new " + this.name + " broadcast enabled");
		}
		
		public function setInput(input:String):void{
			this.send("setup " + this.name + " input " + input );
		}
		
		public function setOutput(output:String):void{
			this.send("setup " + this.name + " output " + output );
		}
		
		public function setOption(option:String, value:String):void{
			this.setFullOption( option + "=" + value);
		}
				
		public function setFullOption(fullOption:String):void{
			this.send("setup " + this.name + " option " + fullOption );
		}
		
		public function send(command:String):void{
			this.connect();
			try{
				this.s.writeUTFBytes(command+"\n");
				this.s.flush();	
			}
			catch (e:Error){
				trace("Error:" + e.toString());
				Alert.show("write failed on socket" + s.remoteAddress + ":" + s.remotePort);
				Alert.show(s.readUTFBytes(s.bytesAvailable));
			}
			//Alert.show(command, "Sent");
		}
		
		public function clean():void{
			this.send("del " + this.name);
		}
		public function show():void{
			s.addEventListener(ProgressEvent.SOCKET_DATA,recv);
			this.send("show " + this.name);
		}
		
		public function start():void{
			this.send("control " + this.name + " play");	
		}
		
		public function pause():void{
			this.send("control " + this.name + " pause");
		}
		
		public function stop():void{
			this.send("control " + this.name + " stop");  
			//this.kill();
			//dispatchEvent(new Event(Event.COMPLETE));
		}
		
		public function kill():void{
			this.send("shutdown");
		}
		
		public function disconnect():void{
			if (s.connected) s.close();
		}
		
		
	}
	
}